/**
 * 
 */
package com.jie.eclipse.pan.editors.configuration;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

import org.eclipse.jface.text.IAutoEditStrategy;
import org.eclipse.jface.text.contentassist.IContentAssistProcessor;
import org.eclipse.jface.text.source.ISourceViewer;
import org.eclipse.jst.jsp.core.text.IJSPPartitions;
import org.eclipse.jst.jsp.ui.StructuredTextViewerConfigurationJSP;
import org.eclipse.jst.jsp.ui.internal.contentassist.JSPContentAssistProcessor;
import org.eclipse.jst.jsp.ui.internal.contentassist.JSPELContentAssistProcessor;
import org.eclipse.jst.jsp.ui.internal.contentassist.JSPJavaContentAssistProcessor;
import org.eclipse.jst.jsp.ui.internal.contentassist.NoRegionContentAssistProcessorForJSP;
import org.eclipse.wst.html.core.text.IHTMLPartitions;
import org.eclipse.wst.sse.core.text.IStructuredPartitions;
import org.eclipse.wst.xml.core.internal.provisional.text.IXMLPartitions;

import com.jie.eclipse.pan.editors.assist.processor.BaseJSPContentAssistProcessor;
import com.jie.eclipse.pan.editors.assist.processor.PanJSPContentAssistantProcessor;
import com.jie.eclipse.pan.editors.assist.processor.PanhtmlContentAssistProcessor;
import com.jie.eclipse.pan.utils.Utils;

/**
 * @author yanjie
 *
 */
public class JSPStructuredTextViewerConfiguration extends StructuredTextViewerConfigurationJSP{
	/* 
	 * 提供自定义的自动提示策略
	 * 
	 * @see org.eclipse.jst.jsp.ui.StructuredTextViewerConfigurationJSP#getContentAssistProcessors(org.eclipse.jface.text.source.ISourceViewer, java.lang.String)
	 */
	@SuppressWarnings({ "deprecation", "restriction" })
	protected IContentAssistProcessor[] getContentAssistProcessors(ISourceViewer sourceViewer, String partitionType) {
		
		IContentAssistProcessor[] processors = null;
		//我们目前只自定义JSP标签属性值自动提示的情况，所以针对的分区类型为IJSPPartitions.JSP_DIRECTIVE
		if (partitionType == IJSPPartitions.JSP_DIRECTIVE) {
			processors = new IContentAssistProcessor[]{new PanJSPContentAssistantProcessor()};
		}else if ((partitionType == IXMLPartitions.XML_DEFAULT) || (partitionType == IHTMLPartitions.HTML_DEFAULT) || (partitionType == IHTMLPartitions.HTML_COMMENT) || (partitionType == IJSPPartitions.JSP_DEFAULT) || (partitionType == IJSPPartitions.JSP_DIRECTIVE) || (partitionType == IJSPPartitions.JSP_CONTENT_DELIMITER) || (partitionType == IJSPPartitions.JSP_CONTENT_JAVASCRIPT) || (partitionType == IJSPPartitions.JSP_COMMENT)) {
			// jsp
			processors = new IContentAssistProcessor[]{new JSPContentAssistProcessor()};
		}else if ((partitionType == IXMLPartitions.XML_CDATA) || (partitionType == IJSPPartitions.JSP_CONTENT_JAVA)) {
			// jsp java
			processors = new IContentAssistProcessor[]{new JSPJavaContentAssistProcessor()};
		}else if (partitionType == IJSPPartitions.JSP_DEFAULT_EL) {
			// jsp el
			processors = new IContentAssistProcessor[]{new JSPELContentAssistProcessor()};
		}else if (partitionType == IStructuredPartitions.UNKNOWN_PARTITION) {
			// unknown
			processors = new IContentAssistProcessor[]{new NoRegionContentAssistProcessorForJSP()};
		}else{
			processors = super.getContentAssistProcessors(sourceViewer, partitionType);
		}
		
		return processors;
	}
	
	/* 
	 * 提供自定义的自动编辑策略
	 * 
	 * @see org.eclipse.jst.jsp.ui.StructuredTextViewerConfigurationJSP#getAutoEditStrategies(org.eclipse.jface.text.source.ISourceViewer, java.lang.String)
	 */
	public IAutoEditStrategy[] getAutoEditStrategies(ISourceViewer sourceViewer, String contentType) {
		//Utils.print("bbb");
		
		//我们目前只定义JSP标签有关的自动提示策略，所以针对的分区类型为IJSPPartitions.JSP_DIRECTIVE
		if (contentType == IJSPPartitions.JSP_DIRECTIVE) {
			List<IAutoEditStrategy> strategies = new ArrayList<IAutoEditStrategy>();
			
			//WTP已配置的自动编辑策略
			IAutoEditStrategy[] existingStrategies = super.getAutoEditStrategies(sourceViewer, contentType);
			Collections.addAll(strategies, existingStrategies);
			
			//自定义的自动编辑策略
//			IAutoEditStrategy customizedStrategies = new JSPDirectivePartitionAutoEditStrategy();
//			strategies.add(customizedStrategies);
			
			return strategies.toArray(new IAutoEditStrategy[strategies.size()]);
		}
		
		return super.getAutoEditStrategies(sourceViewer, contentType);
	}
	

}
